<!--
 * @Author: cgl
 * @Date: 2023-04-18 15:12:26
 * @LastEditors: cgl
 * @LastEditTime: 2023-07-13 15:20:50
 * @FilePath: \public-health-emergencies-fed:\radiation-hygiene-fe\src\views\person\radiationSite.vue
 * @Description:
 *
 * Copyright (c) 2023 by cgl/微盛信息产业有限公司, All Rights Reserved.
-->
<template>
  <div class="app-container">
    <el-form
      class="searchform"
      :model="queryParams"
      ref="queryParams"
      size="small"
      label-width="100px"
    >
      <el-row>
        <el-col :span="5">
          <el-form-item label="单位名称" label-width="80px">
            <m-select
              v-model="queryParams.entrustOrganizationId"
              placeholder="输入文字检索查询"
              style="width: 100%"
              filterable
              clearable
              :lists="entrustOrganizationSelect"
              lists_label="organizationName"
              lists_value="entrustOrganizationId"
              lists_key="entrustOrganizationId"
              :total="orgTotal"
              :pageSize="10"
              @handleChange="listeningEvent"
            >
            </m-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="场所名称">
            <el-input
              v-model="queryParams.location"
              placeholder="请输入"
              clearable
              @keyup.enter.native="getList()"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="建立时间" prop="formDate">
            <el-date-picker
              v-model="queryParams.formDate"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              clearable
              style="width: 100%"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label-width="20px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList()"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="tableoperation">
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click.native.prevent="openDialog()"
        >添加</el-button
      >
      <el-button
        type="primary"
        icon="el-icon-download"
        size="mini"
        plain
        @click.native.prevent="handleExport()"
        >导出</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      :data="radiationSiteList"
      :height="height - 400 + 'px'"
      @row-dblclick="(row) => openDialog(row, '3')"
    >
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column
        label="单位名称"
        align="center"
        prop="entrustOrganizationName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="使用场所" align="center" prop="location" />
      <el-table-column
        label="检测位置"
        align="center"
        prop="detectionLocation"
      />
      <el-table-column label="关联设备" align="center" prop="equipmentNames" />
      <el-table-column
        label="建立时间"
        align="center"
        prop="venueResumeTime"
        width="150"
      />
      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <el-button @click.native.prevent="openDialog(row)" type="text">
            编辑
          </el-button>
          <el-button
            @click.native.prevent="handleDelete(row)"
            type="text"
            style="color: #f32d2d"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      v-dialogDrag="true"
    >
      <el-form
        :model="form"
        :disabled="status == '3'"
        :rules="rules"
        ref="form"
        size="small"
        label-width="120px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="委托单位" prop="entrustOrganizationId">
              <el-select
                v-model="form.entrustOrganizationId"
                placeholder="输入文字检索查询"
                filterable
                clearable
                style="width: 100%"
              >
                <el-option
                  :label="item.organizationName"
                  :value="item.entrustOrganizationId"
                  v-for="(item, index) in entrustOrganizationSelect"
                  :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="场所位置">
              <el-input
                v-model="form.location"
                :placeholder="status != '3' ? '请输入' : ''"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="场所建立时间">
              <el-date-picker
                clearable
                size="small"
                v-model="form.venueResumeTime"
                type="date"
                value-format="yyyy-MM-dd"
                :placeholder="status != '3' ? '请选择' : ''"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="检测位置">
              <el-input
                v-model="form.detectionLocation"
                :placeholder="status != '3' ? '请输入' : ''"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button type="primary" v-if="status != '3'" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  radiationSiteList,
  radiationSiteExport,
  radiationSiteDetail,
  radiationSiteAdd,
  radiationSiteEdit,
  radiationSiteDelete,
} from "@/api/person/radiationSite";
import {
  entrustOrganizationListing,
  entrustOrganizationList,
} from "@/api/person/entrustOrganization";
import { getToken } from "@/utils/auth";
import addOrganization from "./addOrganization.vue";
import MSelect from "@/components/mSelect";

export default {
  name: "User",
  dicts: ["sys_user_sex"],
  components: { MSelect, addOrganization },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
      rules: {
        entrustOrganizationId: [
          { required: true, message: "委托单位不能为空", trigger: "blur" },
        ],
      },
      list: [
        { label: "1", value: "1" },
        { label: "2", value: "2" },
        { label: "3", value: "3" },
      ],
      // 视图高度
      height: document.documentElement.clientHeight,
      // 场所列表
      radiationSiteList: [],
      // 表单参数
      form: {},
      loading: false,
      status: null,
      title: null,
      // 委托单位下拉框
      entrustOrganizationSelect: [],
      // 单位查询数据
      organizationQueryData: {
        organizationName: "", //单位名称模糊查询
        pageNum: 1, //页码
        pageSize: 10, //每页展示的条数
      },
      orgTotal: 0,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取场所列表
    getList() {
      this.queryParams.venueResumeTimeStart =
        this.queryParams.formDate && this.queryParams.formDate[0];
      this.queryParams.venueResumeTimeEnd =
        this.queryParams.formDate && this.queryParams.formDate[1];
      let queryParams = { ...this.queryParams };
      delete queryParams.formDate;
      this.loading = true;
      radiationSiteList(queryParams)
        .then((response) => {
          this.radiationSiteList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((response) => {
          this.loading = false;
        });
    },
    // 获取单位下拉框
    listeningEvent(e) {
      let { queryName, pageNum, pageSize, type } = e;
      if (type == "scroll" && pageNum == this.organizationQueryData.pageNum)
        return;
      this.organizationQueryData.organizationName = queryName;
      this.organizationQueryData.pageNum = pageNum;
      this.organizationQueryData.pageSize = pageSize;
      entrustOrganizationList(this.organizationQueryData).then((response) => {
        if (type == "scroll") {
          this.entrustOrganizationSelect.push(...response.rows);
        } else {
          this.entrustOrganizationSelect = response.rows;
        }
        this.orgTotal = response.total;
      });
    },
    // 切换tab栏
    changeTab(tab, event) {
      this.queryParams.calibrationState = tab.name;
      this.getList();
    },
    // 打开对话框
    openDialog(row, status) {
      this.resetForm("form");
      if (status == "3") {
        /* 查看 */
        radiationSiteDetail(row.radiationSiteId).then((response) => {
          this.form = response.data;
          this.dialogVisible = true;
        });
        this.title = "查看场所";
        this.status = "3";
      } else {
        if (row) {
          radiationSiteDetail(row.radiationSiteId).then((response) => {
            this.form = response.data;
            this.dialogVisible = true;
          });
          this.status = "2";
          this.title = "编辑场所";
        } else {
          this.form = {};
          this.status = "1";
          this.title = "添加场所";
          this.dialogVisible = true;
        }
      }
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) return;
        if (this.status == "1") {
          radiationSiteAdd(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.dialogVisible = false;
            this.getList();
          });
        } else if (this.status == "2") {
          radiationSiteEdit(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.getList();
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm(`是否确认删除放射场所[${row.location}]？`)
        .then(() => {
          this.loading = true;
          return radiationSiteDelete(row.radiationSiteId);
        })
        .then(() => {
          this.loading = false;
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 导出
    handleExport() {
      this.download(
        "/basic/entrust/radiationSite/export",
        {
          ...this.queryParams,
        },
        `放射场所.xlsx`
      );
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.resetForm("queryParams");
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-dialog__body .el-form {
  width: 60%;
  margin: 0 auto;
}
</style>
